package net.towee.client.com.lobby.view;

import net.towee.client.com.lobby.presenter.LobbyNicknamePresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.i18n.ToweeMessages;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.ExtFlowPanel;
import net.towee.client.widgets.SheduledInputBox;
import net.towee.client.widgets.SpanPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.inject.Inject;


public class LobbyNicknameView extends FlowPanel implements Display {
	private SheduledInputBox inputBox;
	private Button nextStep;
	private Button closeWizard;
	private ExtFlowPanel testResult;
	
	private final ToweeMessages messages;
	private final ClientResource resource;
	private final ToweeConstants constants;
	
	@Inject
	public LobbyNicknameView(ClientResource resource, ToweeConstants constants, ToweeMessages messages){
		this.resource = resource;
		this.messages = messages;
		this.constants = constants;
		
		this.inputBox = new SheduledInputBox();
		this.nextStep = new Button(constants.lobbyFirstStepContinue());
		this.closeWizard = new Button(constants.lobbyClose());
		
		createHelloText();
		createInputBox();
		createEnterTips();
		createHelpText();
		createButtons();
	}
	
	private void createEnterTips() {
		FlowPanel tipsContainer = new FlowPanel();
	}

	private void createButtons() {
		FlowPanel container = new FlowPanel();
		container.add(closeWizard);
		container.add(nextStep);
		add(container);
		
		container.getElement().getStyle().setProperty("marginTop", "25px");
		nextStep.getElement().getStyle().setProperty("marginLeft", "233px");
		
		nextStep.setStyleName( resource.css().lobbyNextStepButton() );
		closeWizard.setStyleName( resource.css().lobbyCloseButton() );
	}

	private void createHelpText() {
		ExtFlowPanel widget = new ExtFlowPanel( constants.lobbyFirstStepContinueMotivation(), true );
		widget.setStyleName(resource.css().normalText());
		add(widget);
	}

	private void createInputBox() {
		FlowPanel inputContainer = new FlowPanel();
		inputContainer.add(inputBox);
		inputContainer.setStyleName(resource.css().roundedInputBox());
		inputContainer.setWidth("350px");
		inputContainer.getElement().getStyle().setProperty("paddingRight", "110px");
		
		SpanPanel towee = new SpanPanel("@towee.net");
		towee.getElement().getStyle().setProperty("position", "absolute");
		towee.getElement().getStyle().setProperty("right", "9px");
		towee.getElement().getStyle().setProperty("top", "3px");
		towee.getElement().getStyle().setProperty("color", "#aaa");
		inputContainer.add(towee);
		add(inputContainer);
		
		testResult = new ExtFlowPanel("&nbsp", true);
		testResult.getElement().getStyle().setProperty("margin", "18px 0px 0px 6px");
		add(testResult);
	}

	private void createHelloText() {
		ExtFlowPanel widget = new ExtFlowPanel( constants.welcomeUser() );
		widget.setStyleName(resource.css().h2());
		add(widget);
		
		widget = new ExtFlowPanel( constants.enterYourNickname(), true );
		widget.setStyleName(resource.css().normalText());
		add(widget);		
	}

	@Override
	public HasChangeHandlers getNicknameChanger() {
		return inputBox;
	}

	@Override
	public HasClickHandlers getNextStepButton() {
		return nextStep;
	}
	
	@Override
	public HasClickHandlers getCloseWizardButton() {
		return closeWizard;
	}

	@Override
	public String getEnteredNickname() {
		return inputBox.getValue();
	}

	@Override
	public void nicknameNotUsed() {
		testResult.getElement().getStyle().setProperty("color", "green");
		testResult.setText(constants.nicknameNotUsed());
		inputBox.getParent().getElement().getStyle().setProperty("border", "2px solid green");
	}

	@Override
	public void nicknameUsed() {
		testResult.getElement().getStyle().setProperty("color", "red");
		testResult.setText(constants.nicknameUsed());
		inputBox.getParent().getElement().getStyle().setProperty("border", "2px solid red");
	}

	@Override
	public void nicknameUpdated() {
		inputBox.getParent().getElement().getStyle().setProperty("background", "#ddd");
		inputBox.getParent().getElement().getStyle().setProperty("border", "2px solid #aaa");
		inputBox.getElement().getStyle().setProperty("background", "#ddd");
		inputBox.setEnabled(false);
		testResult.setHTML("&nbsp;");
	}

	@Override
	public void invalidCharacter() {
		testResult.getElement().getStyle().setProperty("color", "red");
		testResult.setText(constants.invalidCharacterInNickname());
		inputBox.getParent().getElement().getStyle().setProperty("border", "2px solid red");		
	}

	@Override
	public void setNickname(String text) {
		inputBox.setText(text);
	}

}
